package com.sinaapp.gavinzhang.eighth;

import org.junit.Test;

/**
 * 在包含至少一个正数的数组里，其数组最大子串和应该是整数
 * Created by gavin on 15-8-12.
 */
public class SearchMax {
    @Test
    public void test(){
        int[] tmp = {31,-41,59,26,-53,58,97,-93,-23,84};//187
        //int[] tmp = {-1,-2,1,-4,-5};
        //int[] tmp = {-5,-4,-3,-2,-1};
        //int[] tmp = {1, -2, 3, 10, -4, 7, 2, -5};
        //getMax(tmp);
        getMaxByMax(tmp);
    }

    //@Test
    public void getMax(int tmp[])
    {
        int maxEndHere = 0;
        int maxSoFar = 0;
        for (int i = 0; i < tmp.length; i++) {
            maxEndHere = Math.max(0,maxEndHere+tmp[i]);
            maxSoFar = Math.max(maxEndHere,maxSoFar);
        }
        System.out.println(maxSoFar);
    }

    public void getMaxByMax(int tmp[])
    {//将数组中的最大值设置为最大子向量和
        int maxEndHere = 0;
        int maxSoFar = Integer.MIN_VALUE;
        for (int i = 0; i < tmp.length; i++) {
            maxEndHere = Math.max(97,maxEndHere+tmp[i]);
            maxSoFar = Math.max(maxEndHere,maxSoFar);
        }
        System.out.println(maxSoFar-97);
    }


}
